package io.gitee.zhangbinhub.admin.common

import io.gitee.zhangbinhub.acp.boot.interfaces.LogAdapter
import io.gitee.zhangbinhub.acp.cloud.AcpCloudLogAutoConfiguration
import io.gitee.zhangbinhub.acp.cloud.lock.DistributedLock
import io.gitee.zhangbinhub.admin.common.lock.ZkDistributedLock
import org.apache.curator.framework.CuratorFramework
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.Primary

/**
 * @author zhang by 30/09/2019
 * @since JDK 11
 */
@AutoConfiguration(after = [AcpAdminComponentAutoConfiguration::class])
@ConditionalOnMissingBean(DistributedLock::class)
@Import(AcpCloudLogAutoConfiguration::class)
class AcpAdminDistributedLockAutoConfiguration {
    @Bean
    @Primary
    @ConditionalOnBean(CuratorFramework::class)
    fun zkDistributedLock(curatorFramework: CuratorFramework, logAdapter: LogAdapter): DistributedLock =
        ZkDistributedLock(curatorFramework, logAdapter)
}